package com.erci.service.Impl;

import com.erci.constant.MessageConstant;
import com.erci.entity.DTO.LoginDTO;
import com.erci.entity.User;
import com.erci.exception.BaseException;
import com.erci.mapper.LoginMapper;
import com.erci.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.security.auth.login.AccountNotFoundException;
import java.time.LocalDateTime;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;
    @Override
    public User login(LoginDTO loginDTO) {
        String username = loginDTO.getUsername();
        String password = loginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        User user = loginMapper.login(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (user == null) {
            //账号不存在
            throw new BaseException( "monika:账号不存在");
        }

        //密码比对
        // TODO对前端传过来的明文密码进行md5加密处理
        password= DigestUtils.md5DigestAsHex(password.getBytes());

        if (!password.equals(user.getPassword())) {
            //密码错误
            throw new BaseException("monika:密码错误");
        }

        return user;
    }
}
